package com.zeus.web.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 *
 * @title DateTimeUtils
 * @date 2018/1/25
 */
public final class DateTimeUtils {

    private DateTimeUtils() {
    }

    public static void main(String[] args) {
        System.out.println("nowOfLong: " + nowOfLong());
        System.out.println("nowOfInteger: " + nowOfInteger());
        System.out.println("nowOfDate: " + nowOfDate());
        System.out.println("2016-07-31: " + parseStringToDate("2016-07-31", "yyyy-MM-dd"));
        System.out.println("int time to util date: " + intTimeToUtilDate(nowOfInteger()));
        System.out.println("int time to local date: " + intTimeToLocalDate(nowOfInteger()));
    }

    public static long nowOfLong() {
        return System.currentTimeMillis();
    }

    public static int nowOfInteger() {
        return Integer.parseInt(nowOfLong() / 1000 + "");
    }

    public static Date nowOfDate() {
        return localDateTimeToUtilDate(LocalDateTime.now());
    }

    public static Date parseStringToDate(String text, String pattern) {
        if (text.length() > 10) {
            LocalDateTime localDateTime = LocalDateTime.parse(text, DateTimeFormatter.ofPattern(pattern));
            return localDateTimeToUtilDate(localDateTime);
        }

        LocalDate localDate = LocalDate.parse(text, DateTimeFormatter.ofPattern(pattern));
        return localDateToUtilDate(localDate);
    }

    public static Date intTimeToUtilDate(long time) {
        return new Date(time * 1000);
    }

    public static LocalDate intTimeToLocalDate(long time) {
        return utilDateToLocalDate(intTimeToUtilDate(time));
    }

    public static LocalDateTime intTimeToLocalDateTime(long time) {
        return utilDateToLocalDateTime(intTimeToUtilDate(time));
    }

    public static LocalTime intTimeToLocalTime(long time) {
        return utilDateToLocalTime(intTimeToUtilDate(time));
    }

    public static Date localDateTimeToUtilDate(LocalDateTime localDateTime) {
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    public static Date localDateToUtilDate(LocalDate localDate) {
        Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    public static Date localTimeToUtilDate(LocalTime localTime) {
        LocalDateTime localDateTime = LocalDateTime.of(LocalDate.now(), localTime);
        return localDateTimeToUtilDate(localDateTime);
    }

    public static LocalDateTime utilDateToLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    public static LocalDate utilDateToLocalDate(Date date) {
        return utilDateToLocalDateTime(date).toLocalDate();
    }

    public static LocalTime utilDateToLocalTime(Date date) {
        return utilDateToLocalDateTime(date).toLocalTime();
    }
}
